package com.ambow.shop.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.ambow.shop.dao.UserDAO;
import com.ambow.shop.entity.User;
import com.ambow.util.JdbcUtils;

/**
 * 用户的数据访问实现。
 * 
 * @author chunw
 *
 */
public class UserDaoJdbcImpl implements UserDAO {

	@Override
	public int add(User user) throws SQLException {
		final String sql = "INSERT INTO users(userName, userPassword, userRegistrationTime) VALUES(?, ?, ?)";
		try (Connection conn = JdbcUtils.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) {
			stmt.setString(1, user.getName());
			stmt.setString(2, user.getPassword());
			stmt.setDate(3, new Date(user.getRegistrationTime().getTime()));
			return stmt.executeUpdate();
		}
	}

}
